// pages/chooseRecord/chooseRecord.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    records: [],
    selected: []
  },

  handleSend: function (e) {
    let res = [];
    for (let i = 0; i < this.data.selected.length; i++) {
      if (this.data.selected[i] === 1) {
        res.push(this.data.records[i].recordInfo.consultRecordId);
      }
    }
    //把选中的id放到缓存中
    wx.setStorageSync('selectedIds', res);
    wx.navigateBack({
      delta: 1,
    });
  },

  handleChoose: function (e) {
    let idx = e.target.dataset.idx;
    let temp = this.data.selected;
    temp[idx] = 1;
    this.setData({
      selected: temp
    })
  },

  handleCancel: function (e) {
    let idx = e.target.dataset.idx;
    let temp = this.data.selected;
    temp[idx] = 0;
    this.setData({
      selected: temp
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    let userId = wx.getStorageSync("userId");
    wx.showLoading({
      title: '加载中',
    })    
    wx.request({
      url: app.globalData.prefix+'/record/getConsultRecordsByVisitorId/' + userId,
      success: (res) => {
        console.log(res);
        let len = res.data.data.length;
        //不能把当前的咨询也放进来，需要去掉，理论上就是最后一个
        res.data.data.splice(len - 1, 1);
        //按日期降序排列
        res.data.data.sort(function (r1, r2) {
          let time1 = new Date(r1.recordInfo.datetime.replace(/-/g,"/")).getTime();
          let time2 = new Date(r2.recordInfo.datetime.replace(/-/g,"/")).getTime();
          return time2 - time1;
        });
        let temp = new Array(len - 1);
        temp.fill(0);
        this.setData({
          records: res.data.data,
          selected: temp
        })
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})